<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectDataGoods: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoods()),
    SelectDataGoodsGroup: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectDataGoodsGroup()
      ),
    SelectDataGoodsClassify: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectDataGoodsClassify()
      ),
  },
  data() {
    return {
      appWindow: window,
      // 选择商品参数
      form: {
        // 活动名称
        activityName: "",
        // 活动状态 1未开始 2进行中 3已结束
        activityStatus: null,
        // 活动时间 (开始时间 - 结束时间)
        startTimeStr: "",
        endTimeStr: "",
        // freightType	 1满X元 2满X件
        freightType: 1,

        // 下单可用抵扣
        deductOverlayLst: [],

        // 适用分组
        freightProductGroupsLst: [],
        // 适用类目
        freightProductCategoryLst: [],

        // 适用商品
        freightProductLst: [],
        applyType: 1,

        // freightItemLst   层级数组
        freightItemLst: [
          {
            // 活动条件
            conditionValue: "",
            // actionType	1包邮 2减邮费
            actionType: 1,
            // 满减多少元
            discountValue: "",
            // areaInfo	配送区域 areaName 区域名称
            areaInfo: {},
            areaName: "",
          },
        ],
      },
      selectDeliverAreaIndex: null,
      // 页面接口地址
      apiUrl: {
        create: "api/freight/createFreight",
        update: "api/freight/updateFreight",
        detail: "api/freight/getFreight",
      },

      deducteList: [], //抵扣的资产类型的数组

      isDisabled: false,
    };
  },
  methods: {
    // 编辑修改的参数扩展方法
    mixinEdit_editDataExtend(data_) {
      console.error("data_", data_);
      // 添加
      if (this.mixinEdit_editType == "add") {
        this.isDisabled = false;
      }
      // 修改
      else if (this.mixinEdit_editType == "update") {
        if (data_.activityStatus === 2) {
          this.isDisabled = true;
        }
      }
      // 复制
      else if (this.mixinEdit_editType == "copy") {
        this.isDisabled = false;
      }

      if (
        this.mixinEdit_editType == "update" ||
        this.mixinEdit_editType == "copy"
      ) {
        // 处理返回的抵扣资产id的数组
        this.disposeIntegralType(data_);
        // 处理返回的优惠券对应商品内容
        if (!this.$vFN.Utils.isEmpty(data_.freightProductGroupsLst)) {
          let newArr = [];
          newArr.push(data_.freightProductGroupsLst[0].groupsId);

          console.error("woc");
          if (data_.freightProductGroupsLst[0].subGroupsId) {
            newArr.push(data_.freightProductGroupsLst[0].subGroupsId);
          }

          data_.freightProductGroupsLst = newArr;
        }
        if (!this.$vFN.Utils.isEmpty(data_.freightProductCategoryLst)) {
          let newArr = [];
          newArr.push(data_.freightProductCategoryLst[0].categoryId);

          if (data_.freightProductCategoryLst[0].subCategoryId) {
            newArr.push(data_.freightProductCategoryLst[0].subCategoryId);
          }

          data_.freightProductCategoryLst = newArr;
        }
        if (!this.$vFN.Utils.isEmpty(data_.freightProductLst)) {
          data_.freightProductLst = data_.freightProductLst.map((item) => {
            return item.productId;
          });
        }

        //
        if (typeof data_.freightItemLst.areaInfo != "object") {
          data_.freightItemLst.forEach((item) => {
            item.areaInfo = JSON.parse(item.areaInfo);
          });
        }
      }

      return data_;
    },

    // form 提交添加/修改 的参数扩展方法
    mixinEdit_editFormSubmitExtend(data_) {
      if (data_.applyType == 2) {
        data_.freightProductGroupsLst = [
          {
            groupsId: data_.freightProductGroupsLst[0],
            subGroupsId: data_.freightProductGroupsLst[1],
          },
        ];

        // 分类请空
        data_.freightProductCategoryLst = [];
      } else if (data_.applyType == 3) {
        data_.freightProductCategoryLst = [
          {
            categoryId: data_.freightProductCategoryLst[0],
            subCategoryId: data_.freightProductCategoryLst[1],
          },
        ];

        // 分组请空
        data_.freightProductGroupsLst = [];
      } else if (data_.applyType == 4) {
        let _list = [];
        data_.freightProductLst.forEach((item) => {
          _list.push({
            productId: item,
          });
        });
        data_.freightProductLst = _list;

        // 分组和分类请空
        data_.freightProductCategoryLst = [];
        data_.freightProductGroupsLst = [];
      }

      // 配送区域格式转换
      let _list2 = [];
      data_.freightItemLst.map((item) => {
        if (typeof item.areaInfo == "object") {
          item.areaInfo = JSON.stringify(item.areaInfo);
        }
        _list2.push(item);
      });
      data_.freightItemLst = _list2;

      return data_;
    },
    // 添加模板
    addFreightItem() {
      // console.log("添加")
      console.log(this.form.freightItemLst);
      let length_ = this.form.freightItemLst.length;
      let value_ =
        Number(this.form.freightItemLst[length_ - 1].conditionValue) + 1;

      let _d = {
        // 活动条件
        conditionValue: value_,
        // actionTypes	1包邮 2减邮费
        actionType: 1,
        // 满减多少元
        discountValue: "",
        // areaInfo	配送区域 areaName 区域名称
        areaInfo: {},
        areaName: "",
      };
      this.form.freightItemLst.push(_d);
    },
    // 删除模板
    delFreightItem(item, index) {
      this.$delete(this.form.freightItemLst, index);
    },

    selectDeliverArea(item_, index_) {
      let _selectList = [];
      this.form.freightItemLst.forEach((item, index) => {
        if (typeof item.areaInfo == "object" && index_ != index) {
          _selectList.push(item.areaInfo);
        }
      });
      let _selected = this.$vFN.Utils.deepMerge(_selectList);

      this.selectDeliverAreaIndex = index_;
      this.$vFN.Directives.SelectDeliverArea({
        appWindow: this.appWindow,
        list: item_.areaInfo,
        disable: _selected,
        apiConfig: this.$vFN.Tools.getAppGlobalData(
          "componentsApiConfig.SelectDeliverArea",
          this.appWindow
        ),
        onSuccess: (res) => {
          let _areaName = [];
          for (let i in res.list) {
            let _item = res.list[i];
            if (_item.checkAll) {
              _areaName.push(_item.name + "[全境]");
            } else {
              let _a = [];
              for (let ii in _item.list) {
                let _iitem = _item.list[ii];
                _a.push(_iitem.name);
              }
              _areaName.push(_item.name + "[" + _a.join("、") + "]");
            }
          }

          let _name = "";
          if (res.checkAll) {
            _name = "所有地区";
          } else {
            _name = _areaName.join("、");
          }
          this.form.freightItemLst[this.selectDeliverAreaIndex].areaInfo =
            res.list;
          this.form.freightItemLst[this.selectDeliverAreaIndex].areaName =
            _name;
          this.selectDeliverAreaIndex = null;
        },
      });
    },

    // 初始化
    async init() {
      await this.getIntegralType();
      // 初始化数据
      await this.mixinEdit_editBoxInit({
        model: "form",
        id: this.id,
      });

      console.error("fff", this.form);
    },

    async getIntegralType() {
      this.mixinEdit_loading = true;
      let res = await this.$vFN.Http.post(
        {
          url: "api/freight/getIntegralType",
        },
        window
      );
      this.mixinEdit_loading = false;
      if (res._ok) {
        this.deducteList = res.data.map((item) => {
          return {
            ...item,
            isChecked: false,
          };
        });
        console.log("deducteList", this.deducteList);
      }
    },

    // 处理返回的资产id数组  判断是否选中
    disposeIntegralType(data_) {
      this.form.deductOverlayLst = data_.deductOverlayLst;
      // console.log("this.deducteList", this.deducteList);
      // console.log("this.form.deductOverlayLst", this.form.deductOverlayLst);

      this.deducteList.forEach((item) => {
        item.id = String(item.id);
        let findIndex = this.form.deductOverlayLst.indexOf(item.id);
        // console.log("index", index);
        if (findIndex > -1) {
          item.isChecked = true;
        }
      });

      // console.log("this.deducteList", this.deducteList);
    },

    minNumber(index_) {
      // console.log("index_", index_);
      let num = 0;
      if (index_ > 0) {
        num = this.form.freightItemLst[index_ - 1].conditionValue + 1;
      } else {
        num = 1;
      }
      // console.log("num", num);
      return num;
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>